/*
 * @Author: Xavier 1305433186@qq.com
 * @Date: 2024-08-09 15:13:34
 * @LastEditors: Xavier 1305433186@qq.com
 * @LastEditTime: 2024-08-16 16:51:09
 * @FilePath: \JS32T031_SDK\User\433_decoding\433_software_decoding.c
 * @Description:
 *
 * STAYTRUE (c) 2024 by ${Xavier}, All Rights Reserved.
 */

#include "433_decoding/433_software_decoding.h"
#include <string.h>

wireless_433_data_t wireless_433_data;
wireless_433_config_t wireless_433_config;
unsigned char get_433_key_stroke(unsigned char *key_stroke)
{
    char press_count = wireless_433_data.press_count;
    if (wireless_433_data.decryption_successful == true && (wireless_433_data.ir_stok == false || wireless_433_data.press_count >= 10))
    {
        wireless_433_data.decryption_successful = false;
        memcpy(key_stroke, wireless_433_data.byte, WIRELESS_433_BYTE);
        return press_count;
    }
    else
    {
        return false;
    }
}
void decode_433_io_init(wireless_433_config_t *p_config_t)
{
    wireless_433_config.get_io = p_config_t->get_io;
}
void decode_433(void)
{
    if (wireless_433_data.ir_time > START_SIGNAL_MAX_TIME)
    {
        wireless_433_data.ir_stok = false;
        wireless_433_data.low_count_time_sign = true;
        wireless_433_data.higt_count_time_sign = true;
        wireless_433_data.raw_data_last = 0;
    }
    else
    {
        wireless_433_data.ir_time++;
    }
    if (wireless_433_config.get_io() == true)
    {
        // 时间间隔 更新
        if (wireless_433_data.low_count_time_sign == true)
        {
            wireless_433_data.low_count_time_sign = false;
            wireless_433_data.higt_count_time_sign = true;
            wireless_433_data.low_time = wireless_433_data.ir_time;
            wireless_433_data.ir_time = 0;
            if (wireless_433_data.ir_stok == true)
            {
                if (wireless_433_data.higt_time + wireless_433_data.low_time >= LOW_LEVEL_MIN_TIME + HIGT_LEVEL_MIN_TIME &&
                    wireless_433_data.higt_time + wireless_433_data.low_time <= LOW_LEVEL_MAX_TIME + HIGT_LEVEL_MAX_TIME)
                {
                    // 数据 为0
                    if (wireless_433_data.higt_time >= LOW_LEVEL_MIN_TIME &&
                        wireless_433_data.higt_time <= LOW_LEVEL_MAX_TIME)
                    {
                        wireless_433_data.raw_data = wireless_433_data.raw_data << 1;
                        wireless_433_data.raw_data_len++;
                    }
                    // 数据 为1
                    else if (wireless_433_data.higt_time >= HIGT_LEVEL_MIN_TIME &&
                             wireless_433_data.higt_time <= HIGT_LEVEL_MAX_TIME)
                    {
                        wireless_433_data.raw_data = wireless_433_data.raw_data << 1;
                        wireless_433_data.raw_data += 0x00000001;
                        wireless_433_data.raw_data_len++;
                    }
                    else
                    {
                        wireless_433_data.ir_stok = false;
                    }
                    if (wireless_433_data.raw_data_len >= WIRELESS_433_BYTE * 8)
                    {
                        if (wireless_433_data.raw_data_last == wireless_433_data.raw_data)
                        {
                            wireless_433_data.press_count++;
                        }
                        else
                        {
                            wireless_433_data.press_count = 1;
                        }
                        wireless_433_data.raw_data_last = wireless_433_data.raw_data;
                        for (size_t i = 0; i < WIRELESS_433_BYTE; i++)
                        {
                            wireless_433_data.byte[i] = (char)wireless_433_data.raw_data;
                            wireless_433_data.raw_data = wireless_433_data.raw_data >> 8;
                        }
                        wireless_433_data.decryption_successful = true;
                        // wireless_433_data.ir_stok = false;
                    }
                }
                else
                {
                    wireless_433_data.ir_stok = false;
                }
            }
            // 启始信号 判断
            if (wireless_433_data.low_time >= START_SIGNAL_MIN_TIME && wireless_433_data.low_time <= START_SIGNAL_MAX_TIME && wireless_433_data.ir_stok == false)
            {
                wireless_433_data.ir_stok = true;
                wireless_433_data.raw_data_len = 0;
                wireless_433_data.raw_data = 0;
            }
        }
    }
    else
    {
        // 时间间隔 更新
        if (wireless_433_data.higt_count_time_sign == true)
        {
            wireless_433_data.low_count_time_sign = true;
            wireless_433_data.higt_count_time_sign = false;
            wireless_433_data.higt_time = wireless_433_data.ir_time;
            wireless_433_data.ir_time = 0;
        }
    }
}
